package com.sinodata.bsm.cicp.collector.net.collector;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.collector.net.collector.NetinterfacesCollector.IfInterfaceInfo;
import com.sinodata.bsm.cicp.collector.net.collector.NetinterfacesCollector.NetInterfaceVo;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 链路上行速率(kb/s)    = (下行接口的输出速率+上行接口输入速率)/2
 * 链路下行速率(kb/s)    = (下行接口的输入速率+上行接口输出速率)/2
 * 链路上行利用率(kb/s)  = (上行速率/容量)*100 %
 * 链路下行利用率 (kb/s) = (下行速率/容量)*100 %
 * 链路状态 = (上行,下行都是up的时候链路才是up)
 */
public class NetLinkCollector extends Collector {
    private static Logger logger = Logger.getLogger(NetLinkCollector.class);

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String upInfo = getParamMap().get("上行接口"); //ip,index,community
        String downInfo = getParamMap().get("下行接口");
        String linkSpeed = getParamMap().get("链路带宽");
        String linkInfo = "upInfo=" + upInfo + "\ndownInfo=" + downInfo + "\nlinkMtu=" + linkSpeed;
        try {
            String up[] = upInfo.split(",");
            String down[] = downInfo.split(",");
            NetInterfaceVo upIfVo = (NetInterfaceVo) getPersistentData("value" + up[0].trim());
            NetInterfaceVo downIfVo = (NetInterfaceVo) getPersistentData("value" + down[0].trim());
            IfInterfaceInfo upIfInfo = null;
            IfInterfaceInfo downIfInfo = null;
            if (upIfVo != null && downIfVo != null) {
                upIfInfo = upIfVo.getIfIntervalues().get(up[0].trim() + ":" + up[1].trim());
                downIfInfo = downIfVo.getIfIntervalues().get(down[0].trim() + ":" + down[1].trim());
            }
            if (upIfInfo != null && downIfInfo != null) {

                List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
                String[] propMetas = getPropMeta();
                ResPropertyValue propertyValueX = new ResPropertyValue();
                ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];

                Long upspeed = (upIfInfo.getIfInVelocity() + downIfInfo.getIfOutVelocity()) / 2;
                Long downspeed = (upIfInfo.getIfOutVelocity() + downIfInfo.getIfInVelocity()) / 2;
                Double uppct = 0D;
                Double downpct = 0D;
                if (Long.parseLong(linkSpeed) != 0) {
                    uppct = upspeed * 100.00 / Long.parseLong(linkSpeed);
                    downpct = downspeed * 100.00 / Long.parseLong(linkSpeed);
                }
                String status = "up".equals(upIfInfo.getIfStatus()) && "up".equals(downIfInfo.getIfStatus()) ? "up" : "down";

                java.text.NumberFormat nf = java.text.NumberFormat.getNumberInstance(java.util.Locale.CHINA);
                nf.setMaximumFractionDigits(2);

                propValues[0] = ResPropertyValue.createPropValue(upspeed + "", propMetas[0]);
                propValues[1] = ResPropertyValue.createPropValue(downspeed + "", propMetas[1]);
                propValues[2] = ResPropertyValue.createPropValue(nf.format(uppct).replace(",", ""), propMetas[2]);
                propValues[3] = ResPropertyValue.createPropValue(nf.format(downpct).replace(",", ""), propMetas[3]);
                propValues[4] = ResPropertyValue.createPropValue(status, propMetas[4]);

                propertyValueX.setValues(propValues);
                values.add(propertyValueX);

                logger.debug("Link collect param\n" + linkInfo + "\nLink property value\n" + propertyValueX.toString());

                if (upspeed < 0 || downspeed < 0 || uppct < 0 || downpct < 0)
                    return null;

                return values;
            }

        } catch (Exception e) {
            logger.error("Failed to collect the data of link, linkInfo: " + linkInfo, e);
            throw new CollectorException("Failed to collect the data of link, linkInfo: " + linkInfo, e);
        }

        return null;
    }

    public String[] getParamMeta() {
        return new String[] { "上行接口", "下行接口", "链路带宽" };
    }

    public String[] getPropMeta() {
        return new String[] { "链路上行速率", "链路下行速率", "链路上行利用率", "链路下行利用率", "链路状态" };
    }

    //	private IfEntry getIfEntry(String ip,String community,String index) throws Exception {
    //		SnmpGet get = new SnmpGet(ip);
    //		get.setCommunity(community);
    //		List<IfEntry> ifTable = new ArrayList<IfEntry>();
    //		SnmpUtil.setValues(get, ifTable, IfEntry.class, "1.3.6.1.2.1.2.2");
    //		for(IfEntry ife:ifTable) {
    //			if(ife.getIfIndex().equals(index)) return ife;
    //		}
    //		return null;
    //	}

    public static void main(String[] args) {

    }

}
